package com.zfl;

import java.util.ArrayList;
import java.util.List;

public class Generate {

    public static void main(String[] args) {
        int numRows = 5;
        List<List<Integer>> lists = new Generate().generate(numRows);
        System.out.println("lists = " + lists);
    }

    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        ArrayList<Integer> row1 = new ArrayList<>();
        row1.add(1);
        res.add(row1);
        System.out.println("res = " + res);
        if (numRows==1) {
            return res;
        }
        ArrayList<Integer> dp = new ArrayList<>(row1);
        ArrayList<Integer> temp = new ArrayList<>();
        for (int i = 1; i < numRows ; i++) {
            temp = new ArrayList<>(dp);
            dp.clear();
            for (int j = 0; j <=i ; j++) {
                if (j==0||j==i) {
                    dp.add(1);
                } else {
                    dp.add(temp.get(j)+temp.get(j-1)) ;
                }
            }
            res.add(new ArrayList<>(dp));
            System.out.println("lists = " + res);
        }
        return res;
    }
}
